package com.a0soft.gphone.aTruffleHog.wnd.info;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Patrick Cheng <patrick.taiwan (at) gmail.com>
 *
 */

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfo;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfoKey;
import com.a0soft.gphone.aTruffleHog.util.CONSTS;

public class InfoSTView {
	private static final int MAIN_VERSION_TH = 0x00010000;
    private static final String ST_KEYS[] = {
            DeviceInfoKey.K.ST_BOARD,
            DeviceInfoKey.K.ST_BOOTLOADER,
            DeviceInfoKey.K.ST_BRAND,
            DeviceInfoKey.K.ST_DEVICE,
            DeviceInfoKey.K.ST_DISPLAY,
            DeviceInfoKey.K.ST_FINGERPRINT,
            DeviceInfoKey.K.ST_HARDWARE,
            DeviceInfoKey.K.ST_HOST,
            DeviceInfoKey.K.ST_ID,
            //DeviceInfoKey.K.ST_MID, //don't show model_id
            DeviceInfoKey.K.ST_MANUFACTURE,
            DeviceInfoKey.K.ST_MODEL,
            DeviceInfoKey.K.ST_PRODUCT,
            DeviceInfoKey.K.ST_RADIO,
            DeviceInfoKey.K.ST_TAGS,
            DeviceInfoKey.K.ST_TIME,
            DeviceInfoKey.K.ST_TYPE,
            DeviceInfoKey.K.ST_USER,
            DeviceInfoKey.K.ST_CODENAME,
            DeviceInfoKey.K.ST_INCREMENTAL,
            DeviceInfoKey.K.ST_RELEASE,
            DeviceInfoKey.K.ST_SDK_INT,
            DeviceInfoKey.K.ST_VM_VERSION,
            DeviceInfoKey.K.ST_LINUX_VERSION,
            DeviceInfoKey.K.ST_ROM_DEVNAME,
            DeviceInfoKey.K.ST_ROM_NAME,
            DeviceInfoKey.K.ST_ROM_VERSION,
            DeviceInfoKey.K.ST_ROOTED,
            DeviceInfoKey.K.ST_FEATURES,
            DeviceInfoKey.K.ST_GLES_VERSION,
            DeviceInfoKey.K.ST_TOUCH_SCREEN,
            DeviceInfoKey.K.ST_KEYBOARD,
            DeviceInfoKey.K.ST_NAVIGATION,
    };
    
    private Context _cxt = null;
    private DeviceInfo _di = null;
    
    public
    InfoSTView(Context cxt, DeviceInfo di)
    {
    	_cxt = cxt;
    	_di = di;
    }
    
    public View
    getView(boolean for_preview)
    {
        LinearLayout sv = new LinearLayout(_cxt);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        sv.setLayoutParams(params);
        sv.setOrientation(LinearLayout.VERTICAL);
        
        int childno = 0;
        for (int i=0; i<ST_KEYS.length; i++) {
            String info = _di.GetString(ST_KEYS[i]);
            if (!TextUtils.isEmpty(info) && !info.equals(CONSTS.UNAVAILABLE)) {
                View row = LayoutInflater.from(_cxt).inflate(R.layout.info_item,null);
                InfoItemViewWrapper wrapper = new InfoItemViewWrapper(row);
                row.setTag(wrapper);
                wrapper.setItemName(DeviceInfoKey.GetName(ST_KEYS[i]));
                if (TextUtils.equals(ST_KEYS[i], DeviceInfoKey.K.ST_FEATURES)) {
                	// JSON array
    				JSONArray features = _di.GetJSONArray(ST_KEYS[i]);
    				if (features != null && features.length() > 0) {
        				String [] fstr_array = new String[features.length()];
        				for (int j=0; j<features.length(); j++) {
        					try {
        						fstr_array[j] = features.getString(j);
        					}
        					catch (JSONException e1) {
							}
        				}
        				java.util.Arrays.sort(fstr_array);
    				    info = "";
        				for (int j=0; j<features.length(); j++) {
   						    if (j != 0)
   						        info += "\n";
   						    info += fstr_array[j];
        				}
    				}
                }
                else if (TextUtils.equals(ST_KEYS[i], DeviceInfoKey.K.ST_GLES_VERSION)) {
                	int gle_version = _di.GetInt(ST_KEYS[i]);
                	int main_version = gle_version / MAIN_VERSION_TH;
                	int sub_version = gle_version % MAIN_VERSION_TH;
                	info = "v"+main_version+"."+sub_version;
                }
                else if (TextUtils.equals(ST_KEYS[i], DeviceInfoKey.K.ST_TOUCH_SCREEN)) {
                	info = getTouchScreenString(_di.GetInt(ST_KEYS[i]));
                }
                else if (TextUtils.equals(ST_KEYS[i], DeviceInfoKey.K.ST_KEYBOARD)) {
                	info = getKeyboardString(_di.GetInt(ST_KEYS[i]));
                }
                else if (TextUtils.equals(ST_KEYS[i], DeviceInfoKey.K.ST_NAVIGATION)) {
                	info = getNavigationString(_di.GetInt(ST_KEYS[i]));
                }
                else if (TextUtils.equals(ST_KEYS[i], DeviceInfoKey.K.ST_TIME)) {
                	info = DateUtils.formatDateTime(_cxt, _di.GetLong(ST_KEYS[i]), 
                			DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR);
                }
                wrapper.setItemDetail(info);
                sv.addView(row);
                childno++;
            }               
        }
        return sv;
    }
    
    private String
    getTouchScreenString(int type)
    {
    	if (type > 3)
    		type = 0;
    	return _cxt.getResources().getStringArray(R.array.di_st_tss)[type];
    }

    private String
    getKeyboardString(int type)
    {
    	if (type > 2)
    		type = 0;
    	return _cxt.getResources().getStringArray(R.array.di_st_kbs)[type];
    }
    
    private String
    getNavigationString(int type)
    {
    	if (type > 4)
    		type = 0;
    	return _cxt.getResources().getStringArray(R.array.di_st_nvs)[type];
    }
}
